/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.tb_certificate_vehicles.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.SysIndirectInt;
import com.thinkgem.jeesite.modules.sys.service.SysIndirectIntService;

import com.thinkgem.jeesite.modules.tb_car_info.entity.TbCarInfo;
import com.thinkgem.jeesite.modules.tb_car_info.service.TbCarInfoService;
import com.thinkgem.jeesite.modules.tb_certificate_vehicles.entity.TbCertificateVehicles;
import com.thinkgem.jeesite.modules.tb_certificate_vehicles.service.TbCertificateVehiclesService;

/**
 * 车辆证件审核时间表Controller
 * @author ZhaoHai
 * @version 2017-07-06
 */
@Controller
@RequestMapping(value = "${adminPath}/tb_certificate_vehicles/tbCertificateVehicles")
public class TbCertificateVehiclesController extends BaseController {

	@Autowired
	private TbCertificateVehiclesService tbCertificateVehiclesService;
	
	@Autowired
	private TbCarInfoService tbCarInfoService;
	
	@Autowired
	private SysIndirectIntService sysIndirectIntService;
	
	@ModelAttribute
	public TbCertificateVehicles get(@RequestParam(required=false) String id) {
		TbCertificateVehicles entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tbCertificateVehiclesService.get(id);
		}
		if (entity == null){
			entity = new TbCertificateVehicles();
		}
		return entity;
	}

	@RequiresPermissions("tb_certificate_vehicles:tbCertificateVehicles:view")
	@RequestMapping(value = {"list", ""})
	public String list(TbCertificateVehicles tbCertificateVehicles, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TbCertificateVehicles> page = tbCertificateVehiclesService.findPage(new Page<TbCertificateVehicles>(request, response), tbCertificateVehicles); 
		model.addAttribute("page", page);
		return "modules/tb_certificate_vehicles/tbCertificateVehiclesList";
	}

	@RequiresPermissions("tb_certificate_vehicles:tbCertificateVehicles:view")
	@RequestMapping(value = "form")
	public String form(TbCertificateVehicles tbCertificateVehicles, Model model) {
		TbCertificateVehicles tbcertificateVehicles = new TbCertificateVehicles();
		List<TbCertificateVehicles> tbCertificateVehicles1=tbCertificateVehiclesService.findList(tbcertificateVehicles);
		List<String> list = new ArrayList<String>();
		for (TbCertificateVehicles tbCertificateVehicles2 : tbCertificateVehicles1) {
			list.add(tbCertificateVehicles2.getCarNum());
		}
		tbCertificateVehicles.setTbCarInfo(new TbCarInfo());
		model.addAttribute("tbCertificateVehicles", tbCertificateVehicles);
		model.addAttribute("tbCarInfo", tbCarInfoService.besidesCarnum(list));
		return "modules/tb_certificate_vehicles/tbCertificateVehiclesForm";
	}

	@RequiresPermissions("tb_certificate_vehicles:tbCertificateVehicles:edit")
	@RequestMapping(value = "save")
	public String save(TbCertificateVehicles tbCertificateVehicles, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tbCertificateVehicles)){
			return form(tbCertificateVehicles, model);
		}
		tbCertificateVehiclesService.save(tbCertificateVehicles);
		addMessage(redirectAttributes, "保存车辆证件审查时间表成功");
		return "redirect:"+Global.getAdminPath()+"/tb_certificate_vehicles/tbCertificateVehicles/?repage";
	}
	
	@RequiresPermissions("tb_certificate_vehicles:tbCertificateVehicles:edit")
	@RequestMapping(value = "delete")
	public String delete(TbCertificateVehicles tbCertificateVehicles, RedirectAttributes redirectAttributes) {
		tbCertificateVehiclesService.delete(tbCertificateVehicles);
		addMessage(redirectAttributes, "删除车辆证件审查时间表成功");
		return "redirect:"+Global.getAdminPath()+"/tb_certificate_vehicles/tbCertificateVehicles/?repage";
	}
	/**
	 * 件检测日期小于 倒计时天数的证件和车辆信息的页面
	 * @return ArrayList<TbCertificateVehicles>
	 */
	@RequiresPermissions("tb_certificate_vehicles:tbCertificateVehicles:view")
	@RequestMapping(value = "timedifferences")
	public String certificatesalarm(Model model){
		ArrayList<Map<String, String>>  list=tbCertificateVehiclesService.certificatesalarm();
		model.addAttribute("list", list);
		return "modules/tb_certificate_vehicles/certificatesExpire";
	}
	/**
	 * 修改提前提醒期限 
	 */
	@RequestMapping(value = "advanceValue")
	public String advanceValue(@RequestParam("values") String values,HttpServletRequest request, HttpServletResponse response,RedirectAttributes redirectAttributes){
		SysIndirectInt sysIndirectInt=new SysIndirectInt();
		sysIndirectInt.setId("1");
		sysIndirectInt.setContentValue(values);
		sysIndirectIntService.save(sysIndirectInt);
		addMessage(redirectAttributes, "修改提前提醒天数成功");
		return "redirect:"+Global.getAdminPath()+"/tb_certificate_vehicles/tbCertificateVehicles/?repage";
	}	
}